#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

typedef long long LL;
const int N = 1e9 + 10, MOD = 1e9+ 7;
LL n, k;
LL qpow(LL a, LL b, LL p)
{
	LL sum = 1;
	while (b)
	{
		if (b & 1) sum = (sum * a) % p;
		b >>= 1;
		a = a * a % p;
	}
	return sum;
}
// ((k + 1)^ (n + 1) - k - 1) / k
int main() 
{
	int q; cin >> q;
	while (q--)
	{
		cin >> n >> k;
		LL p = MOD;
		LL t1 = qpow(k + 1, n + 1, p);
		LL t2 = ((t1 - k - 1) % p + p) % p;
		LL t3 = qpow(k, p - 2, p);
		LL ret = (t2 % p * t3 % p) % p;
		cout << ret << endl;
	}
	
	return 0;
}